//
//  SourcesOrderListVC.m
//  NtoccShipper
//
//  Created by mac on 2019/11/30.
//  Copyright © 2019 zhuchongjin. All rights reserved.
//

#import "SourcesOrderListVC.h"
#import "SourcesOrderListCell.h"
#import "SourcesOrderListModel.h"
#import "SourcesOrderListDetailVC.h"
#import "MyInPutAlertView.h"
#import "CommentVC.h"
#import "UnLoadSureVC.h"
#import "LoadSureVC.h"
#import "AccountPriceVC.h"
#import "UnLoadGoodsVC.h"
#import "ComplaintVC.h"
#import "SeeCommentVC.h"
#import "LoadGoodsVC.h"
#import "AdjustOrdersVC.h"
#import "ComplainsRecordsVC.h"
#import "ApplyOpenInvoicListVC.h"
#import "YuFuMoneyView.h"
#import "ApplyForPayment.h"
#import "MyCancleSureAlertView.h"
#import "StrongReminderView.h"
#import "ConsolidatedPaymentController.h"
#import "RemarkViewController.h"
#import "StaffAuditorViewController.h"
#import "NYSExcDescView.h"

@interface SourcesOrderListVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) CJTableView *tableView;
@property (nonatomic,strong) NSMutableArray *modelArray;
@property (nonatomic,strong) NSString *refreshType; //刷新
@property (nonatomic,readwrite) int page;
@property (copy, nonatomic) NSString *strStatus; // 货源状态
//@property (strong, nonatomic) NSDictionary *dicSearch;
@property (nonatomic, strong) CJButton *consolidatedButton;
@property (nonatomic, strong) CJButton *staffButton;
@property (nonatomic, strong) NYSExcDescView *exDescView;

@end

@implementation SourcesOrderListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor cjBackgroundColor];
    DLog(@"======%@",self.indexPage);

    [self.view addSubview:self.tableView];
    WS(ws);
    self.tableView.placeHolderView = [[KKTableViewNoDataView alloc] initWithFrame:self.tableView.bounds image:[UIImage imageNamed:@"no_data"] viewClick:^{
           [ws.tableView.mj_header beginRefreshing];
    }];
    
    [self initData];
    [self loadData];
    
    CJRegisterNotify(@"searchOrderdListAction", @selector(searchOrderdListAction:))

}
- (CJButton *)consolidatedButton{
    if (!_consolidatedButton) {
        _consolidatedButton = CJButton.btnInit
        .btnFrame(CGRectMake(50,CGRectGetMaxY(self.tableView.frame) + 10,SCREEN_WIDTH - 100,50))
        .btnText(@"合并打款")
        .btnNormalTitleColor([UIColor cjWhiteColor])
        .btnBgColor([UIColor cjMainColor])
        .btnTargetAction(self, @selector(btnCancleAction:))
        .btnCornerRadius(10)
        .btnCornerWidth(1)
        .btnCornerColor([UIColor cjMainColor])
        ;
    }
    return _consolidatedButton;
}
- (CJButton *)staffButton{
    if (!_staffButton) {
        _staffButton = CJButton.btnInit
        .btnFrame(CGRectMake(50,CGRectGetMaxY(self.tableView.frame) + 10,SCREEN_WIDTH - 100,50))
        .btnText(@"批量核算")
        .btnNormalTitleColor([UIColor cjWhiteColor])
        .btnBgColor([UIColor cjMainColor])
        .btnTargetAction(self, @selector(staffbtnCancleAction:))
        .btnCornerRadius(10)
        .btnCornerWidth(1)
        .btnCornerColor([UIColor cjMainColor])
        ;
    }
    return _staffButton;
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
//    [self.tableView.mj_header beginRefreshing];
    if ([CJControl isLogin]) {
        if ([CJControl getIdCardDateHasDue] && [CJControl getStrongReminderShow]) {
            [[[StrongReminderView shared]initWithView] show];
        }else{
            [[[StrongReminderView shared]initWithView] hidden];
        }
    }

}

- (void)searchOrderdListAction:(NSNotification *)notification{
//      BiddDriverListModel *list = (BiddDriverListModel *)notification.object
//    self.dicSearch = (NSDictionary *)notification.object;
    [self.tableView.mj_header beginRefreshing];
}

- (void)initData{
    self.modelArray = [[NSMutableArray alloc]init];
    self.page = 1;
}

/// <#Description#>
- (void)loadData {
//    @[@"全部",@"未接单",@"已接单",@"已装货",@"已卸货",@"已核算",@"已完成""]
//    self.titleArray = @[@"全部",@"已接单",@"已装货",@"已卸货",@"未核算",@"待审核",@"已核算",@"已完成"];
//    self.titles = @[@"0",@"1",@"2",@"3",@"3",@"6",@"4",@"5"];
    if (self.indexPage.intValue == 0) {
        self.strStatus = @"";
    }else if (self.indexPage.intValue == 1){
        //已接单
        self.strStatus = @"1";
    }else if (self.indexPage.intValue == 2){
        //"已装货"
        self.strStatus = @"2";
    }else if (self.indexPage.intValue == 3){
       //"已卸货"
        self.strStatus = @"3";
    }else if (self.indexPage.intValue == 7){
        //"未核算"
         self.strStatus = @"3";
     }else if (self.indexPage.intValue == 10){
        //"待处理异常运单"
        self.strStatus = @"3";
    }else if (self.indexPage.intValue == 4){
        //"已核算"
        self.strStatus = @"4";
    }else if (self.indexPage.intValue == 5){
       //"已完成"
        self.strStatus = @"5";
    }else if (self.indexPage.intValue == 6){
        //"待审核"
         self.strStatus = @"6";
    }
//    else if (self.indexPage.intValue == 6){
//        //"已完成"
//        self.strStatus = @"6";
//
//    }
//    else if (self.indexPage.intValue == 7){
//        //"已撤销"
//        self.strStatus = @"";
//        self.strIsCancel = @"1";
//
//    }else if (self.indexPage.intValue == 8){
//
//        // "常发货源"
//        self.strStatus = @"";
//        self.strIsOftenOrder = @"1";
//
//    }
    
//    NSDictionary *dicPra = @{
//        @"mainOrderNumber":self.tfMainOrderNumber.text,
//        @"placeOfShipment":self.tfPlaceOfShipment.text,
//        @"placeOfReceipt":self.tfPlaceOfReceipt.text,
//        @"coalTypeName":self.tfCoalTypeName.text,
//        @"orderCode":self.tfOrderCode.text,
//        @"driverInfo":self.tfDriverInfo.text,
//        @"vehicleLicense":self.tfVehicleLicense.text,
//        @"bageNums":@(bageNums)
//    };
    
    if (self.indexPage.intValue == 10) {
        [self networingErrorList];
    }else{
        [self networkingList];
    }
}
-(void)networingErrorList{
    NSString *strURL = CJStringWithFormat(@"%@/ntocc-tms-api/orderDetail/orderDetailHandlePageList",CJBaseUrl);
    NSDictionary *dicPar = @{
        @"pageNum":CJStringWithFormat(@"%d",self.page),
         @"pageSize":@"8",
         @"status":self.strStatus,
        @"isUnloadingConfirm":@"1",
        @"isCannotAccounting":@"1",
         @"mainOrderCode":self.strCode,
//                                 @"isOftenOrder":self.strIsOftenOrder,
//                                 @"isCancel":self.strIsCancel,
         @"mainOrderNumber":[CJControl getThreeUnaryValueStr:self.dicSearch[@"mainOrderNumber"]],
         @"placeOfShipment":[CJControl getThreeUnaryValueStr:self.dicSearch[@"placeOfShipment"]],
         @"placeOfReceipt":[CJControl getThreeUnaryValueStr:self.dicSearch[@"placeOfReceipt"]],
         @"coalTypeName":[CJControl getThreeUnaryValueStr:self.dicSearch[@"coalTypeName"]],
         @"orderCode":[CJControl getThreeUnaryValueStr:self.dicSearch[@"orderCode"]],
         @"driverInfo":[CJControl getThreeUnaryValueStr:self.dicSearch[@"driverInfo"]],
         @"vehicleLicense":[CJControl getThreeUnaryValueStr:self.dicSearch[@"vehicleLicense"]],
        @"signTimeStart":[CJControl getThreeUnaryValueStr:self.dicSearch[@"startGmtCreated"]],
        @"signTimeEnd":[CJControl getThreeUnaryValueStr:self.dicSearch[@"endGmtCreated"]],
    };

    WS(ws);
    [[HDNetworking sharedHDNetworking] GET:strURL parameters:dicPar success:^(id  _Nonnull responseObject) {

        DLog(@"++++++++%@",responseObject);
        NSArray *list = responseObject[@"records"];
        NSDictionary *dicSum = responseObject[@"sumObject"];
        NSString *total6 = @"0";
        if (self.indexPage.intValue == 6){
            total6 = CJStringWithFormat(@"%@",responseObject[@"total"]);
        }
        NSString *total = CJStringWithFormat(@"%@",responseObject[@"total"]);
        
        NSArray *listTitleNum = [[NSArray alloc]initWithObjects:
                         CJStringWithFormat(@"%@",dicSum[@"allTrans"]),
                         CJStringWithFormat(@"%@",dicSum[@"waitTrans"]),
                         CJStringWithFormat(@"%@",dicSum[@"onTrans"]),
                         CJStringWithFormat(@"%@",dicSum[@"waitSettle"]),
                         CJStringWithFormat(@"%@",@"0"),
                         CJStringWithFormat(@"%@",dicSum[@"waitSettle"]),
//                             CJStringWithFormat(@"%@",dicSum[@"waitCheckTrans"]),
                         CJStringWithFormat(@"%@",@"0"),
                         CJStringWithFormat(@"%@",dicSum[@"waitPay"]),
                         CJStringWithFormat(@"%@",dicSum[@"finishedTrans"]),
                         nil];
        CJSendNotify(CJOrderDetailListBadgeNum, listTitleNum);
        
//            if (list.count > 0) {

            if ([ws.refreshType isEqualToString:@"down"]) {
                [ws.modelArray removeAllObjects];
            }

            NSError * error ;
            NSArray *arr = [SourcesOrderListModel arrayOfModelsFromDictionaries:list error:&error];
            [ws.modelArray addObjectsFromArray:arr];
  
            [ws.tableView.mj_header endRefreshing];
            [ws.tableView.mj_footer endRefreshing];

            if (ws.modelArray.count == total.intValue) {
                [ws.tableView.mj_footer endRefreshingWithNoMoreData];
            }
        
//            }else{
//
//                [ws.tableView.mj_header endRefreshing];
//                [ws.tableView.mj_footer endRefreshingWithNoMoreData];
//            }
        [ws.tableView reloadData];
    } failure:^(NSError * _Nonnull error) {
        DLog(@"++++++++%@",error);
        [ws.tableView.mj_header endRefreshing];
        [ws.tableView.mj_footer endRefreshingWithNoMoreData];
    }];
}
-(void)networkingList{
    NSString *strURL = CJStringWithFormat(@"%@/ntocc-tms-api/orderDetail",CJBaseUrl);
    NSString * isUnloadingConfirmStr = @"";
    NSString * isCannotAccountingStr = @"";
    if (self.indexPage.intValue == 7) {
        isUnloadingConfirmStr = @"1";
        isCannotAccountingStr = @"0";
    }
    NSDictionary *dicPar = @{
        @"pageNum":CJStringWithFormat(@"%d",self.page),
         @"pageSize":@"8",
         @"isUnloadingConfirm":isUnloadingConfirmStr,
         @"isCannotAccounting":isCannotAccountingStr,
         @"status":self.strStatus,
         @"mainOrderCode":self.strCode,
//                                 @"isOftenOrder":self.strIsOftenOrder,
//                                 @"isCancel":self.strIsCancel,
         @"mainOrderNumber":[CJControl getThreeUnaryValueStr:self.dicSearch[@"mainOrderNumber"]],
         @"placeOfShipment":[CJControl getThreeUnaryValueStr:self.dicSearch[@"placeOfShipment"]],
         @"placeOfReceipt":[CJControl getThreeUnaryValueStr:self.dicSearch[@"placeOfReceipt"]],
         @"coalTypeName":[CJControl getThreeUnaryValueStr:self.dicSearch[@"coalTypeName"]],
         @"orderCode":[CJControl getThreeUnaryValueStr:self.dicSearch[@"orderCode"]],
         @"driverInfo":[CJControl getThreeUnaryValueStr:self.dicSearch[@"driverInfo"]],
         @"vehicleLicense":[CJControl getThreeUnaryValueStr:self.dicSearch[@"vehicleLicense"]],
        @"signTimeStart":[CJControl getThreeUnaryValueStr:self.dicSearch[@"startGmtCreated"]],
        @"signTimeEnd":[CJControl getThreeUnaryValueStr:self.dicSearch[@"endGmtCreated"]],
    };

    WS(ws);
    [[HDNetworking sharedHDNetworking] GET:strURL parameters:dicPar success:^(id  _Nonnull responseObject) {

        DLog(@"++++++++%@",responseObject);
        NSArray *list = responseObject[@"records"];
        NSDictionary *dicSum = responseObject[@"sumObject"];
        NSString *total6 = @"0";
        if (self.indexPage.intValue == 6){
            total6 = CJStringWithFormat(@"%@",responseObject[@"total"]);
        }
        NSString *total = CJStringWithFormat(@"%@",responseObject[@"total"]);
        
        NSArray *listTitleNum = [[NSArray alloc]initWithObjects:
                         CJStringWithFormat(@"%@",dicSum[@"allTrans"]),
                         CJStringWithFormat(@"%@",dicSum[@"waitTrans"]),
                         CJStringWithFormat(@"%@",dicSum[@"onTrans"]),
                         CJStringWithFormat(@"%@",dicSum[@"waitSettle"]),
                         CJStringWithFormat(@"%@",@"0"),
                         CJStringWithFormat(@"%@",dicSum[@"waitSettle"]),
//                             CJStringWithFormat(@"%@",dicSum[@"waitCheckTrans"]),
                         CJStringWithFormat(@"%@",@"0"),
                         CJStringWithFormat(@"%@",dicSum[@"waitPay"]),
                         CJStringWithFormat(@"%@",dicSum[@"finishedTrans"]),
                         nil];
        CJSendNotify(CJOrderDetailListBadgeNum, listTitleNum);
        
//            if (list.count > 0) {

            if ([ws.refreshType isEqualToString:@"down"]) {
                [ws.modelArray removeAllObjects];
            }

            NSError * error ;
            NSArray *arr = [SourcesOrderListModel arrayOfModelsFromDictionaries:list error:&error];
            [ws.modelArray addObjectsFromArray:arr];
  
            [ws.tableView.mj_header endRefreshing];
            [ws.tableView.mj_footer endRefreshing];

            if (ws.modelArray.count == total.intValue) {
                [ws.tableView.mj_footer endRefreshingWithNoMoreData];
            }
        
//            }else{
//
//                [ws.tableView.mj_header endRefreshing];
//                [ws.tableView.mj_footer endRefreshingWithNoMoreData];
//            }
        [ws.tableView reloadData];
    } failure:^(NSError * _Nonnull error) {
        DLog(@"++++++++%@",error);
        [ws.tableView.mj_header endRefreshing];
        [ws.tableView.mj_footer endRefreshingWithNoMoreData];
    }];
}

- (void)dealloc{
    CJRemoveNofify;
}

- (void)downRefresh{
    self.refreshType = @"down";
    self.page = 1;
    [self loadData];
}

- (void)upRefresh{
    self.refreshType = @"up";
    self.page ++;
    [self loadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.modelArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *identifier = @"SourcesOrderListCell";
    SourcesOrderListCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[SourcesOrderListCell alloc] initWithFlex:[CJControl getUIWithNew] ? @"SourcesOrderListCellNew":@"SourcesOrderListCell" reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    SourcesOrderListModel *model = self.modelArray[indexPath.row];
    [cell setCellInfoWithModel:model Index:self.indexPage];
    
    WS(ws)
    cell.blockClickPhone = ^(id anyObject) {
//        [[UIApplication sharedApplication]openURL:[NSURL URLWithString:model.driverPhone]];
        [[UIApplication sharedApplication]openURL:[NSURL URLWithString:CJStringWithFormat(@"tel:%@",model.driverPhone)]];
    };
    
    cell.blockCancle = ^(id anyObject) {
        [ws btnTouchCancleModel:model];
    };
    
    cell.blockCancleSure = ^(id anyObject) {
        [ws btnTouchCancleSureModel:model];
    };
    
    cell.blockYuFu = ^(id anyObject) {
        [ws btnTouchYufuModel:model];
    };
    
    cell.blockLoadGoods = ^(id anyObject) {
        [ws btnTouchLoadGoodsModel:model];
    };
    
    cell.blockLoadGoodsSure = ^(id anyObject) {
        [ws btnTouchLoadGoodsSureModel:model];
    };
    
    cell.blockAgainLoadGoods = ^(id anyObject) {
        [ws btnTouchAgainLoadGoodsModel:model];
    };
    
    cell.blockUnloadGoods = ^(id anyObject) {
        [ws btnTouchUnloadGoodsModel:model ];
    };
    
    cell.blockUnloadGoodsSure = ^(id anyObject) {
        [ws btnTouchUnloadGoodsSureModel:model];
    };
    
    cell.blockAgainUnloadGoods = ^(id anyObject) {
        [ws btntouchAgainUnloadGoodsModel:model];
    };
    
    cell.blockAccountPrice = ^(id anyObject) {
        [ws btnAouchAccountPriceModel:model];
    };
    
    cell.blockOpenInvoice = ^(id anyObject) {
        [ws btnTouchOpenInvoiceModel:model];
    };
    
    cell.blockApplyRemit = ^(id anyObject) {
        [ws btnTouchApplyRemitModel:model];
    };
    
    cell.blockEvaluate = ^(id anyObject) {
        [ws btnTouchEvaluateModel:model];
    };
    
    cell.blockSeeEvaluate = ^(id anyObject) {
        [ws btnTouchSeeEvaluateModel:model];
    };
    
    cell.blockAdjustOrder = ^(id anyObject) {
        [ws btnTouchAdjustOrderModel:model];
    };
    
    cell.blockComplaint = ^(id anyObject) {
        [ws btnTouchComplaintModel:model];
    };
    
    cell.blockComplaintRecord = ^(id anyObject) {
        [ws btnTouchComplaintRecordModel:model];
    };
    
    cell.blcokTurnDown = ^(id anyObject) {
        [ws btnTurnDownWithModel:model];
    };
    cell.blcokRemarkTurnDown = ^(id anyObject) {
        RemarkViewController *vc = [[RemarkViewController alloc] init];
        vc.code = model.code;
        vc.remarkStr = model.remark;
        [self pushVc:vc];
    };
    return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    SourcesOrderListModel *model = self.modelArray[indexPath.row];
    SourcesOrderListDetailVC *vc = [[SourcesOrderListDetailVC alloc]init];
    vc.strCode = model.code;
    [self pushVc:vc];
}

#pragma mark ----------btn
/// 取消
/// @param model <#model description#>
- (void)btnTouchCancleModel:(SourcesOrderListModel *)model{

    WS(ws);
    MyInPutAlertView *alert = [[MyInPutAlertView alloc]initWithTitle:@"取消原因" Placeholder:@"请填写取消原因" ClickOnSure:^(NSString *strInPut) {
        
        NSString *strURL = CJStringWithFormat(@"%@/ntocc-tms-api/orderDetail/driverCancelOrder",CJBaseUrl);
        NSDictionary *dicPar = @{
            @"code":model.code,
            @"isCancel":@"1",
            @"driverApplyRemark":strInPut
        };

        [[HDNetworking sharedHDNetworking] PUT:strURL parameters:dicPar success:^(id  _Nonnull responseObject) {
         
            [ws.tableView.mj_header beginRefreshing];

        } failure:^(NSError * _Nonnull error) {
            DLog(@"++++++++%@",error);
         
        }];
    } ClickOnCancle:^(NSString *strCancle) {
            
    }];
    alert.center = KEYWINDOW.center;
    WLAlertView * alertView = [[WLAlertView alloc]init];
    alertView.containerView = alert;
    alertView.isTouchOtherUndissmiss=YES;
    [alertView show];
    
}

/// 取消确认
/// @param model <#model description#>
- (void)btnTouchCancleSureModel:(SourcesOrderListModel *)model{
//    [CJAlert showStringDismissWithDelay:@"取消确认"];
    WS(ws);
    //
    MyCancleSureAlertView *alert = [[MyCancleSureAlertView alloc]initWithTitle:@"处理描述:" cancleReson:model.driverApplyRemark Placeholder:@"请填写描述内容" ClickOnSure:^(NSString * _Nonnull strInPut) {
        
        [ws checkDriverCancleOrder:model isCancelType:@"2" reson:strInPut];
        
    } ClickOnCancle:^(NSString * _Nonnull strCancle) {
        [ws checkDriverCancleOrder:model isCancelType:@"3" reson:strCancle];
    }];
    
     alert.center = KEYWINDOW.center;
     WLAlertView * alertView = [[WLAlertView alloc]init];
     alertView.containerView = alert;
     alertView.isTouchOtherUndissmiss=YES;
     [alertView show];
}

- (void)checkDriverCancleOrder:(SourcesOrderListModel *)model isCancelType:(NSString *)type reson:(NSString *)strReson{

    WS(ws);
    NSString *strURL = CJStringWithFormat(@"%@/ntocc-tms-api/orderDetail/driverCancelOrder",CJBaseUrl);
    NSDictionary *dicPar = @{
                          @"code":model.code,
                          @"isCancel":type,
                          @"shipperDealRemark":strReson
    };

    [[HDNetworking sharedHDNetworking] PUT:strURL parameters:dicPar success:^(id  _Nonnull responseObject) {
     
     DLog(@"++++++++%@",responseObject);
        [ws.tableView.mj_header beginRefreshing];

    } failure:^(NSError * _Nonnull error) {
     DLog(@"++++++++%@",error);
     
    }];
}

#pragma mark ----------预付
/// 预付
/// @param model <#model description#>
- (void)btnTouchYufuModel:(SourcesOrderListModel *)model{
  
    WS(ws);
    [[YuFuMoneyView shared]initWithModel:model ClickOnSure:^(NSString * _Nonnull strSure) {
        [ws.tableView.mj_header beginRefreshing];
    } ClickOnCancle:^(NSString * _Nonnull strCancle) {

    }];
}

/// 装货
/// @param model <#model description#>
- (void)btnTouchLoadGoodsModel:(SourcesOrderListModel *)model{
//    [CJAlert showStringDismissWithDelay:@"装货"];
    WS(ws);
    LoadGoodsVC *vc = [LoadGoodsVC new];
    vc.strCode = model.code;
//    vc.model = model;
    vc.isAgain = NO;
    vc.blcokSucess = ^(id anyObject) {
        [ws.tableView.mj_header beginRefreshing];
    };
    [self pushVc:vc];
}

/// 装货确认
/// @param model <#model description#>
- (void)btnTouchLoadGoodsSureModel:(SourcesOrderListModel *)model{
//    [CJAlert showStringDismissWithDelay:@"装货确认"];
    WS(ws);
    LoadSureVC *vc = [LoadSureVC new];
    vc.strCode = model.code;
    vc.blockSuccess = ^(id anyObject) {
        [ws.tableView.mj_header beginRefreshing];
    };
    [self pushVc:vc];
}

/// 重新装货
/// @param model <#model description#>
- (void)btnTouchAgainLoadGoodsModel:(SourcesOrderListModel *)model{
//    [CJAlert showStringDismissWithDelay:@"重新装货"];
    WS(ws);
    LoadGoodsVC *vc = [LoadGoodsVC new];
    vc.strCode = model.code;
    vc.isAgain = YES;
    vc.blockSucessAgain = ^(id anyObject) {
        [ws.tableView.mj_header beginRefreshing];
    };
    [self pushVc:vc];
}

/// 卸货
/// @param model <#model description#>
- (void)btnTouchUnloadGoodsModel:(SourcesOrderListModel *)model{
//    [CJAlert showStringDismissWithDelay:@"卸货"];
    WS(ws);
    UnLoadGoodsVC *vc = [UnLoadGoodsVC new];
    vc.strCode = model.code;
    vc.strUnLoadGoodsUnite = model.coalUnitName;
    vc.isAgain = NO;
    vc.blcokSucess = ^(id anyObject) {
        [ws.tableView.mj_header beginRefreshing];
    };
    [self pushVc:vc];
}

/// 卸货确认
/// @param model <#model description#>
- (void)btnTouchUnloadGoodsSureModel:(SourcesOrderListModel *)model{
//    [CJAlert showStringDismissWithDelay:@"卸货确认"];
    WS(ws);
    UnLoadSureVC *vc = [[UnLoadSureVC alloc]init];
    vc.strCode = model.code;
    vc.blockSuccess = ^(id anyObject) {
        [ws.tableView.mj_header beginRefreshing];
    };
    [self pushVc:vc];
}

/// 重新卸货
/// @param model <#model description#>
- (void)btntouchAgainUnloadGoodsModel:(SourcesOrderListModel *)model{
//    [CJAlert showStringDismissWithDelay:@"重新卸货"];
    WS(ws);
    UnLoadGoodsVC *vc = [UnLoadGoodsVC new];
    vc.strCode = model.code;
    vc.isAgain = YES;
    vc.blockSucessAgain = ^(id anyObject) {
        [ws.tableView.mj_header beginRefreshing];
    };
    [self pushVc:vc];
}

/// 核算
/// @param model <#model description#>
- (void)btnAouchAccountPriceModel:(SourcesOrderListModel *)model{
//    [CJAlert showStringDismissWithDelay:@"核算"];
    WS(ws);
    AccountPriceVC *vc = [AccountPriceVC new];
    vc.strCode = model.code;
    vc.mainOrderCode = model.mainOrderCode;
    vc.blockSucess = ^(id anyObject) {
        [ws.tableView.mj_header beginRefreshing];
    };
    [self pushVc:vc];
}

/// 开票
/// @param model <#model description#>
- (void)btnTouchOpenInvoiceModel:(SourcesOrderListModel *)model{
//    [CJAlert showStringDismissWithDelay:@"开票"];
    WS(ws);
//    ApplyOpenInvoicVC *vc = [ApplyOpenInvoicVC new];
    ApplyOpenInvoicListVC *vc = [ApplyOpenInvoicListVC new];
    vc.strCode = model.code;
    vc.strOpenType = @"";  // CJStringWithFormat(@"%ld",model.billingType);
    vc.blockSucess = ^(id anyObject) {
        [ws.tableView.mj_header beginRefreshing];
    };
    [self pushVc:vc];
}
- (NYSExcDescView *)exDescView {
    if (!_exDescView) {
        CGFloat width = SCREEN_WIDTH;
        CGFloat height = SCREEN_HEIGHT*0.5 + 60 + 15  + 180 + 80;
        _exDescView = [[NYSExcDescView alloc] initWithFrame:CGRectMake(0, 100, width, height) upcode:@"" Isremit:@"1"];
    }
    return _exDescView;
}
/// 货主打款
/// @param model <#model description#>
- (void)btnTouchApplyRemitModel:(SourcesOrderListModel *)model{
    WS(ws);
    ws.exDescView.code = model.code;
    ws.exDescView.isHightType = @"1";
    ws.exDescView.returnBlock = ^(NSString *typeStr) {
        [ws.exDescView removeFromSuperview];
        ws.exDescView = nil;
    };
    ws.exDescView.block = ^(NSString *code, NSString *merchantCode, NSString *accountType) {
        [self getPreApplicationForPaymentNeedSmsCode:model.code withOrderCode:model.orderCode accountType:accountType];
            [ws.exDescView removeFromSuperview];
             ws.exDescView = nil;
    };
    [[UIApplication sharedApplication].keyWindow addSubview:ws.exDescView];


}


#pragma mark ----------网商打款部分 
/// 货主打款前操作校验返回 是否需要短信验证码 needSmsCode:yes
/// @param strCodes <#strCodes description#>
- (void)getPreApplicationForPaymentNeedSmsCode:(NSString *)strCodes withOrderCode:(NSString *)strOrderCode accountType:(NSString *)type{
    
    NSString *strUrl = CJStringWithFormat(@"%@/ntocc-tms-api/orderDetail/preApplicationForPayment",CJBaseUrl);
    NSDictionary *dicPra = @{
        @"codes":[CJControl getThreeUnaryValueStr:strCodes]
    };
    @weakify(self);
    [[HDNetworking sharedHDNetworking] dataTaskWithHTTPMethod:PostType url:strUrl parameters:dicPra status:ReturnAllData success:^(id  _Nonnull responseObject) {

//        {
//        msg = "本次打款金额为零，可能为月结单，请确定",
//        data =     {
//            payType = "platform",
//            needSmsCode = "no",
//            totalAmount = 0,
//        },
        
        @strongify(self);
        NSDictionary *dic = (NSDictionary *)responseObject;
        NSDictionary *dicData = dic[@"data"];
        NSString *strPayType = dicData[@"payType"];
        
        //   platform   为直接打款
        if ([strPayType isEqualToString:@"platform"]) {
            
            [self btnTouchApplyRemitPlatformAction:strCodes Type:type];
        }else{
            NSString *str = CJStringWithFormat(@"%d",[type intValue] + 1);
            NSString *msg = responseObject[@"msg"];
            [self btnShowPaymentViewWithDic:responseObject[@"data"] withPayTypeMsg:msg withOrderCode:strOrderCode strCode:strCodes Type:str];
        }
        
        
    } failure:^(NSError * _Nonnull error) {
        
    }];
    
}

/// 直接打款
/// @param strCodes <#strCodes description#>
- (void)btnTouchApplyRemitPlatformAction:(NSString *)strCodes Type:(NSString *)type{
    
    CJAlertView *alert = [[CJAlertView alloc]initWithTitle:@"货主打款" strContent:@"你确定要货主打款吗？" ClickOnSure:^(NSString * _Nonnull strSure) {
        NSString *str = CJStringWithFormat(@"%d",[type intValue] + 1);
        NSString *strUrl = CJStringWithFormat(@"%@/ntocc-tms-api/orderDetail/shipperPaymentSure",CJBaseUrl);
        NSDictionary *dicPra = @{
            @"codes":[CJControl getThreeUnaryValueStr:strCodes],
            @"payPlatform":str
        };
        
        @weakify(self);
        [[HDNetworking sharedHDNetworking] dataTaskWithHTTPMethod:PostType url:strUrl parameters:dicPra status:OperationTip success:^(id  _Nonnull responseObject) {
            @strongify(self);
            [self.tableView.mj_header beginRefreshing];

        } failure:^(NSError * _Nonnull error) {
            
        }];
        
    } ClickOnCancle:^(NSString * _Nonnull strCancle) {

    }];
    alert.center = KEYWINDOW.center;
    WLAlertView * alertView = [[WLAlertView alloc]init];
    alertView.containerView = alert;
    alertView.isTouchOtherUndissmiss = YES;
    [alertView show];
    
}


- (void)btnShowPaymentViewWithDic:(NSDictionary *)dic withPayTypeMsg:(NSString *)payType withOrderCode:(NSString *)strOrderCode strCode:(NSString *)strCode Type:(NSString *)type{
    
    WS(ws);
    [[ApplyForPayment shared]initWithPayMentDic:dic withPayTypeMsg:payType codes:strCode wayBillCode:strOrderCode ApreType:type ClickOnSure:^(NSString * _Nonnull strSure) {
        [ws.tableView.mj_header beginRefreshing];
    } ClickOnCancle:^(NSString * _Nonnull strCancle) {
        
    }];
}

/// 评价
/// @param model <#model description#>
- (void)btnTouchEvaluateModel:(SourcesOrderListModel *)model{
//    [CJAlert showStringDismissWithDelay:@"评价"];
    WS(ws);
    CommentVC *vc = [CommentVC new];
    vc.strCode = model.code;
    vc.blockSuccess = ^(id anyObject) {
        [ws.tableView.mj_header beginRefreshing];
    };
    [self pushVc:vc];
}

/// 查看评论
/// @param model <#model description#>
- (void)btnTouchSeeEvaluateModel:(SourcesOrderListModel *)model{
    SeeCommentVC *vc  = [[SeeCommentVC alloc]init];
    vc.strCode = model.code;
    [self pushVc:vc];
}

/// 调单
/// @param model <#model description#>
- (void)btnTouchAdjustOrderModel:(SourcesOrderListModel *)model{
    WS(ws);
    AdjustOrdersVC *vc = [AdjustOrdersVC new];
    vc.blockSucess = ^(id anyObject) {
        NSDictionary *dic = (NSDictionary *)anyObject;
        NSString *strUrl = CJStringWithFormat(@"%@/ntocc-tms-api/orderDetail/dispatchOrderDetail",CJBaseUrl);
        NSDictionary *dicPra = @{
                                 @"code":model.code,
                                 @"driverCode":dic[@"strCode"],
                                 @"vehicleCode":@"carCode"
        };
       
        [[HDNetworking sharedHDNetworking] PUT:strUrl parameters:dicPra success:^(id  _Nonnull responseObject) {
            DLog(@"++++++++%@",responseObject);
            [ws.tableView.mj_header beginRefreshing];
        } failure:^(NSError * _Nonnull error) {
            DLog(@"++++++++%@",error);
            
        }];
    };
    [self pushVc:vc];
}

/// 投诉
/// @param model <#model description#>
- (void)btnTouchComplaintModel:(SourcesOrderListModel *)model{
    WS(ws);
    ComplaintVC *vc = [ComplaintVC new];
    vc.strCode = model.code;
    vc.blockSuccess = ^(id anyObject) {
        [ws.tableView.mj_header beginRefreshing];
    };
    [self pushVc:vc];
}

/// 投诉记录
/// @param model <#model description#>
- (void)btnTouchComplaintRecordModel:(SourcesOrderListModel *)model{
//    [CJAlert showStringDismissWithDelay:@"投诉记录"];
    ComplainsRecordsVC *vc = [[ComplainsRecordsVC alloc]init];
    vc.strCode = model.code;
    [self pushVc:vc];
}

- (void)btnTurnDownWithModel:(SourcesOrderListModel *)model{
    
    CJAlertView *alert = [[CJAlertView alloc]initWithTitle:@"操作提示" strContent:@"确定要驳回核算吗？" ClickOnSure:^(NSString * _Nonnull strSure) {

        NSString *strUrl = CJStringWithFormat(@"%@/ntocc-tms-api/finance/reject",CJBaseUrl);
        NSDictionary *dicPra = @{
            @"codes":model.code
        };
        WS(ws);
        [[HDNetworking sharedHDNetworking] PUT:strUrl parameters:dicPra success:^(id  _Nonnull responseObject) {

            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [ws.tableView.mj_header beginRefreshing];
            });
      

        } failure:^(NSError * _Nonnull error) {
            DLog(@"++++++++%@",error);
        }];

    } ClickOnCancle:^(NSString * _Nonnull strCancle) {

    }];
    alert.center = KEYWINDOW.center;
    WLAlertView * alertView = [[WLAlertView alloc]init];
    alertView.containerView = alert;
    alertView.isTouchOtherUndissmiss = YES;
    [alertView show];
    
    
}


- (void)pushVc:(UIViewController *)vc {
    
    if ([vc isKindOfClass:[UIViewController class]] == NO) return ;
    if (self.navigationController == nil) return ;
    if (vc.hidesBottomBarWhenPushed == NO) {
        vc.hidesBottomBarWhenPushed = YES;
    }
    
    [self.navigationController pushViewController:vc animated:YES];
}

- (CJTableView *)tableView{
    if (!_tableView) {
        if (self.indexPage.intValue == 3) {
            _tableView = CJTableView.tbInitFrameStyle(CGRectMake(0, CJTableToTopHeight, SCREEN_WIDTH, SCREEN_HEIGHT-kTopHeight - kNavBarHeight -CJTableToTopHeight - 70),UITableViewStylePlain)
            .tbDelegate(self)
            .tbDataSource(self)
            .tbSetOther(YES)
            .tbEstimatedRowHeight(YES)
            .tbMjHeadFooter(YES)
            ;
            [self.view addSubview:self.staffButton];
        }else if (self.indexPage.intValue == 4) {
            _tableView = CJTableView.tbInitFrameStyle(CGRectMake(0, CJTableToTopHeight, SCREEN_WIDTH, SCREEN_HEIGHT-kTopHeight - kNavBarHeight -CJTableToTopHeight - 70),UITableViewStylePlain)
            .tbDelegate(self)
            .tbDataSource(self)
            .tbSetOther(YES)
            .tbEstimatedRowHeight(YES)
            .tbMjHeadFooter(YES)
            ;
            [self.view addSubview:self.consolidatedButton];
        }else{
            _tableView = CJTableView.tbInitFrameStyle(CGRectMake(0, CJTableToTopHeight, SCREEN_WIDTH, SCREEN_HEIGHT-kTopHeight - kNavBarHeight -CJTableToTopHeight),UITableViewStylePlain)
            .tbDelegate(self)
            .tbDataSource(self)
            .tbSetOther(YES)
            .tbEstimatedRowHeight(YES)
            .tbMjHeadFooter(YES)
            ;
        }
       
        WS(ws);
        _tableView.blockMjHeader = ^(BOOL isheader) {
            if (isheader) {
                [ws downRefresh];
            } else {
                [ws upRefresh];
            }
        };
    }
    return _tableView;
}
- (void)btnCancleAction:(UIButton *)btn{
    ConsolidatedPaymentController *vc = [[ConsolidatedPaymentController alloc] init];
    
    [self pushVc:vc];
}
- (void)staffbtnCancleAction:(UIButton *)btn{
    StaffAuditorViewController *vc = [[StaffAuditorViewController alloc] init];
    
    [self pushVc:vc];
}
@end
